/**
 * author wen
 * cunyu
 * email wjd_13@hotmail.com
 */
package com.cunyu.service;

import com.cunyu.bean.Form;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * tags 管理服务
 * @author win
 * 2019/04/24-14:19
 */
@Service
public class TagService extends BaseService{

    public List<Map<String, Object>> getTags() {
        String sql = "select * from tag order by type,id" ;
        return getList(sql);
    }

    public Map<String, Object> getTag(int id) {
        String sql = "select * from tag where id = ? " ;
        return getMap(sql,id) ;
    }

    public int updataTag(Form form){
        String sql = "update tag set name = ? ,value = ? ,type = ? ,des = ? where id = ?" ;
        return exec(sql,form.getName(),Integer.valueOf(form.getValue()),
                Integer.valueOf(form.getType()),form.getDes(),Integer.valueOf(form.getId())) ;
    }

    public int newTag(Form form){
        String sql = "insert into tag (name,value,type,des) values (?,?,?,?) " ;
        return insert(sql,form.getName(),Integer.valueOf(form.getValue()),
                Integer.valueOf(form.getType()),form.getDes());
    }

    public Map<String,Map<String,Object>> getTagInfo() {
        String sql = "select * from tag order by type,id" ;
        List<Map<String, Object>> list = getList(sql);

        Map<String,Map<String,Object>> info = new LinkedHashMap<>() ;

        for (Map<String,Object> unit : list){
            String type = unit.get("type").toString();
            if(info.get(type) == null){
                info.put(type,new LinkedHashMap<String, Object>()) ;
            }
            info.get(type).put(unit.get("value").toString(),unit) ;
        }
        return info ;
    }

    public Map<String,Map<String,Object>> getTagNameMap() {
        String sql = "select * from tag order by type,id" ;
        List<Map<String, Object>> list = getList(sql);
        Map<String,Map<String,Object>> info = new LinkedHashMap<>() ;

        for (Map<String,Object> unit : list){
            String type = unit.get("des").toString();
            if(info.get(type) == null){
                info.put(type,new LinkedHashMap<String, Object>()) ;
            }
            info.get(type).put(unit.get("name").toString(),unit) ;
        }
        return info ;
    }
}
